package fruitsManage;

import java.util.ArrayList;
import java.util.Scanner;

public class Sales {

	static ArrayList<SalesModel> sales = new ArrayList<SalesModel>();// 销售记录

	static Scanner scanner = new Scanner(System.in);

	/**
	 * 添加新的销售订单
	 */
	public void addSale() {
		SalesModel salesModel = new SalesModel();
		FruitsModel fruitsModel = new FruitsModel();

		fruitsModel.selectAll();

		System.out.print("请输入要购买的水果名：");
		String name = scanner.nextLine();

		int id = judge(name);// 得到水果编号

		if (id == -1) {
			return;
		}

		// 判断是否还有库存
		if (Fruits.fruits.get(id).getStock() <= 0) {
			System.out.println("对不起，您要购买的水果已售罄！");
			return;
		}

		System.out.print("请输入要购买的数量：");
		double num = Double.parseDouble(scanner.nextLine());
		double stock = Fruits.fruits.get(id).getStock();
		Fruits.fruits.get(id).setStock(stock - num);

		double price = Fruits.fruits.get(id).getPrice();// 得到该水果的价格
		
		System.out.println("========请选择顾客性别：========");
		System.out.println("============1-女性=============");
		System.out.println("============2-男性=============");
		int sex = Integer.parseInt(scanner.nextLine());

		salesModel.addSale(name, price, num, sex);
	}

	/**
	 * 判断是否存在这个水果 如果存在多个同名水果，让用户选择一个
	 * 
	 * @param name
	 * @return
	 */
	public int judge(String name) {
		FruitsModel fruitsModel = new FruitsModel();
		int num = 0, fId = -1;
		for (int i = 0; i < Fruits.fruits.size(); i++) {
			// 找到这种水果
			if (name.equals(Fruits.fruits.get(i).getName())) {
				fId = i;
				num++;
				System.out.println("编号：" + i + "----------------");
				fruitsModel.fruitInformation(Fruits.fruits.get(i));
			}
		}
		// 没找到
		if (num == 0) {
			System.out.println("对不起，没有找到您要购买的水果！");
			return -1;
		}
		// 找到多个
		if (num > 1) {
			System.out.print("请输入您要购买的水果编号：");
			int id = Integer.parseInt(scanner.nextLine());
			return id;
		}
		return fId;
	}

	public void list() {
		SalesModel salesModel = new SalesModel();
		while (true) {
			System.out.println();
			System.out.println("=========请选择========");
			System.out.println("=========1-所有销售记录==");
			System.out.println("=========2-查询销售记录==");
			System.out.println("=========3-退出========");

			int key = Integer.parseInt(scanner.nextLine());
			if (key == 3)
				break;

			switch (key) {
			case 1:
				salesModel.selectAll();
				break;
			case 2:
				System.out.print("请输入水果名：");
				String name = scanner.nextLine();
				salesModel.selectByName(name);
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			scanner.nextLine();
		}
	}

}
